package com.self.campussecondhandmarket.controller.user;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.self.campussecondhandmarket.entity.domain.Recyclers;
import com.self.campussecondhandmarket.resp.Result;
import com.self.campussecondhandmarket.service.RecyclersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * Author: Vincent
 * <p> DateTime: 2024/4/14 22:24
 * <p> Description: 回收商视图层
 * <p>
 **/
@RestController
@RequestMapping("/api/recyclers")
@CrossOrigin(origins = "http://localhost:9999", allowCredentials = "true")
public class RecyclersController implements Serializable {
	
	@Autowired
	RecyclersService recyclersService;
	
	@PostMapping("/all")
	public Result getAll() {
		List<Recyclers> list = recyclersService.list();
		return Result.success(list);
	}
	
	@PostMapping("/del/{id}")
	public Result del(@PathVariable Integer id) {
		recyclersService.removeById(id);
		return Result.success();
	}
	
	@PostMapping("/edit")
	public Result edit(@RequestBody Recyclers recyclers) {
		recyclers.setUpdateTime(null);
		recyclers.setCreateTime(null);
		recyclersService.updateById(recyclers);
		return Result.success();
	}
	
	@PostMapping("/add")
	public Result add(@RequestBody Recyclers recyclers) {
		recyclersService.save(recyclers);
		return Result.success();
	}
	@PostMapping("/admin/filter")
	public Result filter(@RequestBody Recyclers recyclers) {
		List<Recyclers> list = recyclersService.list(Wrappers.<Recyclers>lambdaQuery()
				.eq(recyclers.getCampus() != null, Recyclers::getCampus, recyclers.getCampus())
				.eq(recyclers.getPhone() != null, Recyclers::getPhone, recyclers.getPhone())
				.eq(recyclers.getName() != null, Recyclers::getName, recyclers.getName())
				.eq(recyclers.getWeixin() != null, Recyclers::getWeixin, recyclers.getWeixin())
				.eq(recyclers.getGoods() != null, Recyclers::getGoods, recyclers.getGoods())
		);
		return Result.success(list);
	}
}
